<?php

namespace app\api\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;

use app\admin\model\template\Order as OrderModel;

class AutoCancelOrder extends Command
{

    protected function configure()
    {
        $this->setName('auto_cancel_order')->setDescription('自动取消订单');
    }

    protected function execute(Input $input, Output $output)
    {
        //创建服务
        $server = new \Swoole\Server('127.0.0.1', 29514);
        $server->set(['enable_coroutine' => false]);

        \Swoole\Timer::tick(1000, function (int $timer_id) {
            
            $time = time();
            $orders = OrderModel::where('pay_state', 0)->where('createtime', '<', $time - 15*60)->select();

            foreach ($orders as $v) {
                // 修改订单支付状态
                $v->pay_state = 2;
                $v->save();                
            }
            echo '执行成功' . PHP_EOL;
        });

        //监听数据接收事件。
        $server->on('Receive', function ($server, $fd, $reactor_id, $data) {
            $server->send($fd, "Server: {$data}");
        });

        // 启动服务器
        $server->start();

    }
}